import { Component, OnInit } from '@angular/core';
import { DynamicDialogConfig, DynamicDialogRef, LazyLoadEvent, ConfirmationService } from 'primeng/primeng';
import { MdlDialogService, MdlSnackbarService } from '@angular-mdl/core';
import { HotlineService } from '../hotline.service';
import { DateUtils } from '../../common/utils/DateUtils';
import * as cfg from '../../common/configuration';

@Component({
    templateUrl: './hotline-clean.component.html',
    styleUrls: ['./hotline-clean.component.scss'],
    providers: [HotlineService]
})
export class HotlineCleanComponent implements OnInit {
    zh = cfg.DATE_ZH_CN;
    // 栏目
    cols: any[] = [
        { field: 'yesswNum', header: '热线编号', style: { 'width': '150px', 'text-align': 'center' } },
        { field: 'reporterName', header: '举报人', style: { 'width': '100px', 'text-align': 'center' } },
        { field: 'callNum', header: '来电号码', style: { 'width': '150px', 'text-align': 'center' } },
        { field: 'dispatchType', header: '派遣方式/问题来源', style: { 'width': '150px', 'text-align': 'center' } },
        { field: 'receiveUnitName', header: '处置部门', style: { 'width': '150px', 'text-align': 'center' } },
        { field: 'answerFlagName', header: '专业部门反馈', style: { 'width': '100px', 'text-align': 'center' } },
        { field: 'finishFlagName', header: '问题解决', style: { 'width': '100px', 'text-align': 'center' } },
        { field: 'pleasedFlagName', header: '问题处理满意', style: { 'width': '100px', 'text-align': 'center' } },
        { field: 'cityFinishFlagName', header: '问题解决(市级)', style: { 'width': '150px', 'text-align': 'center' } },
        { field: 'cityPleasedFlagName', header: '问题处理满意(市级)', style: { 'width': '160px', 'text-align': 'center' } }
    ];
    reVisitType = 2;
    timeRangeLabel = '回访时段';
    //数据解析栏目
    items: any[] = [];
    //加载按钮
    loading = false;
    //导入批次日期
    date: any;
    //按钮是否可用
    disabled = false;
    //类型
    type: number = 0;

    totalRecords: number = 0;
    constructor(
        private ref: DynamicDialogRef,
        private config: DynamicDialogConfig,
        private mdlDialogService: MdlDialogService,
        private confirmationService: ConfirmationService,
        private hotlineService: HotlineService) {
        this.type = this.config.data.index;
    }

    ngOnInit() {
        if (1 == this.type) {
            this.timeRangeLabel = '街道接收时间';
        } else if (2 == this.type) {
            this.timeRangeLabel = '部门接收时间';
        }
        this.reVisitTypeChange();
    }

    /**回访类型 */
    reVisitTypeChange() {
        if (this.type == 0) {
            if (this.reVisitType == 1) {
                this.timeRangeLabel = '派单时间';
            } else {
                this.timeRangeLabel = '回访时间';
            }
        }
    }

    /** 查询 */
    search() {
        let params = this.buildParams();
        params['pageSize'] = 30;
        params['pageNum'] = 1;
        this.loading = true;
        this.hotlineService.loadCleanItems(params).subscribe(items => {
            this.loading = false;
            this.items = items.list;
            this.totalRecords = items.total;
        });
    }

    /** 查询 */
    loadLazy(event: LazyLoadEvent) {
        let params = this.buildParams();
        params['pageSize'] = 30;
        params['pageNum'] = event.first / event.rows + 1;
        this.hotlineService.loadCleanItems(params).subscribe(items => {
            this.loading = false;
            this.items = items.list;
            this.totalRecords = items.total;
        });
    }

    /**数据清空 */
    clear() {
        this.confirmationService.confirm({
            message: '您确定要清除当前符合条件的数据共（' + this.totalRecords + '）条吗？',
            accept: () => {
                let params = this.buildParams();
                this.hotlineService.clean(params).subscribe(data => {
                    this.mdlDialogService.alert('数据清理成功！', '确定');
                    this.search();
                });
            }
        });
    }

    // 构建参数
    buildParams() {
        let params = {
            type: this.type,
            revisitType: this.reVisitType
        };
        if (this.date) {
            if (this.date.length >= 1) {
                params['start'] = DateUtils.formatDateTime(this.date[0])
            }
            if (this.date.length == 2) {
                params['end'] = DateUtils.formatDateTime(this.date[1])
            }
        };
        return params;
    }
}